# -*- coding: utf-8 -*-
# created on 2017/8/25

from mathsolver.functions.base import *
from sympy import Intersection


class ShuLieA1RangeUpdate001(BaseFunction):
    """
    已知数列{a_{n}}是首项为正数的等差数列,\\frac{1}{a_{1}•a_{2}}=\\frac{1}{3},\\frac{1}{a_{1}•a_{2}}+\\frac{1}{a_{2}•a_{3}}=\\frac{2}{5},求数列{a_{n}}的通项公式.
    """
    def solver(self, *args):
        assert len(args) == 2
        assert isinstance(args[0], BaseSequence)
        assert isinstance(args[1], str)
        sl = self.search(args[0].name)
        text = args[1]
        if text.find("正") >= 0:
            sl.aRange = Intersection(sl.aRange, Interval(0, S.Infinity, True, True))
            self.output.append(sl)
        else:
            raise Exception("to do")
        return self


class ShuLieA1RangeUpdate002(BaseFunction):
    """
    设数列{a_{n}}的首项a_{1}∈(0,1),a_{n}=\\frac{3-a_{n-1}}{2},求{a_{n}}的通项公式.
    """
    def solver(self, *args):
        assert len(args) == 2
        assert isinstance(args[1], BaseBelong)
        qu_jian = args[1].interval
        shu_lie = self.search(args[0].name)
        shu_lie.aRange = Intersection(shu_lie.aRange, qu_jian)
        self.output.append(shu_lie)
        return self


class ShuLieA1RangeUpdate(BaseFunction):
    CLS = [ShuLieA1RangeUpdate001, ShuLieA1RangeUpdate002]

    def solver(self, *args):
        r = None
        for cl in ShuLieA1RangeUpdate.CLS:
            try:
                r = cl(self.known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
